﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ayxjDllClass
{
    /// <summary>
    /// 控件重绘
    /// </summary>
    public class controlPaint
    {
        #region 
        /// <summary>
        /// GroupBox边框重绘
        /// </summary>
        /// <param name="Control">控件名</param>
        /// <param name="PaintEventArgs">为重绘事件提供数据</param>
        public static void paintGroupBox(Control cnn, PaintEventArgs e)
        {
            e.Graphics.Clear(cnn.BackColor);
            e.Graphics.DrawString(cnn.Text, cnn.Font, Brushes.Black, 10, 1);
            e.Graphics.DrawLine(Pens.Black, 1, 7, 8, 7);
            e.Graphics.DrawLine(Pens.Black, e.Graphics.MeasureString(cnn.Text, cnn.Font).Width + 8, 7, cnn.Width - 2, 7);
            e.Graphics.DrawLine(Pens.Black, 1, 7, 1, cnn.Height - 2);
            e.Graphics.DrawLine(Pens.Black, 1, cnn.Height - 2, cnn.Width - 2, cnn.Height - 2);
            e.Graphics.DrawLine(Pens.Black, cnn.Width - 2, 7, cnn.Width - 2, cnn.Height - 2);
        }
        #endregion

        #region 
        /// <summary>
        /// PictureBox圆角绘制
        /// </summary>
        /// <param name="Control">控件名</param>
        public static void paintPictureBox(Control con)
        {
            GraphicsPath gp = new GraphicsPath();

            gp.AddEllipse(con.ClientRectangle);

            Region region = new Region(gp);

            con.Region = region;

            gp.Dispose();

            region.Dispose();
        }
        #endregion

        #region 示波器
        public void sBqDrawXY(Panel pan)
        {
            Graphics g = pan.CreateGraphics();
            float move = 18f;
            float newX = pan.Width / 2;
            float newY = pan.Height / 2;
            //绘制X轴,  
            PointF px1 = new PointF(move, newY);
            PointF px2 = new PointF(newX * 2 - move, newY);
            g.DrawLine(new Pen(Brushes.Yellow, 1), px1, px2);
            //绘制Y轴  
            PointF py1 = new PointF(newX, move);
            PointF py2 = new PointF(newX, newY * 2 - move);
            g.DrawLine(new Pen(Brushes.Yellow, 1), py1, py2);
        }
        public void sBqDrawYLine(Panel pan, float minY, float maxY, int len)
        {
            float move = 18f;
            float LenX = pan.Width;
            float LenY = pan.Height;
            Graphics g = pan.CreateGraphics();
            PointF px1;
            PointF px2;
            Pen pen;
            string sx;
            StringFormat drawFormat = new StringFormat();
            px1 = new PointF(move, move);
            px2 = new PointF(LenX - move, move);
            sx = maxY.ToString();
            pen = new Pen(Color.Yellow, 1);
            g.DrawLine(pen, px1, px2);
            drawFormat.Alignment = StringAlignment.Far;
            drawFormat.LineAlignment = StringAlignment.Center;
            g.DrawString(sx, new Font("宋体", 8f), Brushes.Yellow, new PointF(move - 2, move), drawFormat);
            for (int i = 1; i < len; i++)    //len等份Y轴  
            {
                px1 = new PointF(move, (LenY - move * 2) * i / len + move);
                px2 = new PointF(LenX - move, (LenY - move * 2) * i / len + move);
                sx = (maxY - (maxY - minY) * i / len).ToString();
                pen = new Pen(Color.Yellow, 1);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
                g.DrawLine(pen, px1, px2);
                drawFormat.Alignment = StringAlignment.Far;
                drawFormat.LineAlignment = StringAlignment.Center;
                g.DrawString(sx, new Font("宋体", 8f), Brushes.Yellow, new PointF(move - 2, (LenY - move * 2) * i / len + move), drawFormat);
            }
            px1 = new PointF(move, (LenY - move * 2) * len / len + move);
            px2 = new PointF(LenX - move, (LenY - move * 2) * len / len + move);
            sx = (-maxY).ToString();
            pen = new Pen(Color.Yellow, 1);
            g.DrawLine(pen, px1, px2);
            drawFormat.Alignment = StringAlignment.Far;
            drawFormat.LineAlignment = StringAlignment.Center;
            g.DrawString(sx, new Font("宋体", 8f), Brushes.Yellow, new PointF(move - 2, (LenY - move * 2) + move), drawFormat);
        }
        public void sBqDrawXLine(Panel pan, float minX, float maxX, int len)
        {
            float move = 18f;
            float LenX = pan.Width;
            float LenY = pan.Height;
            Graphics g = pan.CreateGraphics();
            Pen pen;
            PointF py1;
            PointF py2;
            string sy;
            py1 = new PointF(move, move);
            py2 = new PointF(move, LenY - move);
            sy = minX.ToString();
            pen = new Pen(Color.Yellow, 1);
            g.DrawLine(pen, py1, py2);
            StringFormat drawFormat = new StringFormat();
            drawFormat.Alignment = StringAlignment.Far;
            drawFormat.LineAlignment = StringAlignment.Center;
            g.DrawString(sy, new Font("宋体", 8f), Brushes.Yellow, new PointF(move - 6, LenY - move + 2));
            for (int i = 1; i < len; i++)
            {
                py1 = new PointF((LenX - move * 2) * i / len + move, move);
                py2 = new PointF((LenX - move * 2) * i / len + move, LenY - move);
                sy = ((maxX - minX) * i / len + minX).ToString();
                pen = new Pen(Color.Yellow, 1);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
                g.DrawLine(pen, py1, py2);
                drawFormat.Alignment = StringAlignment.Far;
                drawFormat.LineAlignment = StringAlignment.Center;
                g.DrawString(sy, new Font("宋体", 8f), Brushes.Yellow, new PointF((LenX - move * 2) * i / len + move - 6, LenY - move + 2));
            }
            py1 = new PointF((LenX - move * 2) + move, move);
            py2 = new PointF((LenX - move * 2) + move, LenY - move);
            sy = maxX.ToString();
            pen = new Pen(Color.Yellow, 1);
            g.DrawLine(pen, py1, py2);
            drawFormat.Alignment = StringAlignment.Far;
            drawFormat.LineAlignment = StringAlignment.Center;
            g.DrawString(sy, new Font("宋体", 8f), Brushes.Yellow, new PointF((LenX - move * 2) + move - 6, LenY - move + 2));
        }
        #endregion

        #region 表
        public void qxDrawXY(Panel pan)
        {
            Graphics g = pan.CreateGraphics();
            float move = 20f;
            float newX = pan.Width / 2;
            float newY = pan.Height / 2;
            //绘制X轴,  
            PointF px1 = new PointF(move, newY * 2 - move);
            PointF px2 = new PointF(newX * 2 - move, newY * 2 - move);
            g.DrawLine(new Pen(Brushes.Black, 1), px1, px2);
            //绘制Y轴  
            PointF py1 = new PointF(move, move);
            PointF py2 = new PointF(move, newY * 2 - move);
            g.DrawLine(new Pen(Brushes.Black, 1), py1, py2);
        }
        public void qxDrawYLine(Panel pan, float minY, float maxY, int len)
        {
            float move = 20f;
            float LenX = pan.Width;
            float LenY = pan.Height;
            Graphics g = pan.CreateGraphics();
            PointF px1;
            PointF px2;
            Pen pen;
            string sx;
            StringFormat drawFormat = new StringFormat();
            px1 = new PointF(move, move);
            px2 = new PointF(LenX - move, move);
            sx = (maxY).ToString();
            pen = new Pen(Color.Black, 1);
            g.DrawLine(pen, px1, px2);
            drawFormat.Alignment = StringAlignment.Far;
            drawFormat.LineAlignment = StringAlignment.Center;
            g.DrawString(sx, new Font("宋体", 9f), Brushes.Black, new PointF(move - 2, move), drawFormat);
            for (int i = 1; i < len; i++)    //len等份Y轴  
            {
                px1 = new PointF(move, (LenY - move * 2) * i / len + move);
                px2 = new PointF(LenX - move, (LenY - move * 2) * i / len + move);
                sx = (maxY - (maxY - minY) * i / len).ToString();
                pen = new Pen(Color.Black, 1);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
                g.DrawLine(pen, px1, px2);
                drawFormat.Alignment = StringAlignment.Far;
                drawFormat.LineAlignment = StringAlignment.Center;
                g.DrawString(sx, new Font("宋体", 9f), Brushes.Black, new PointF(move - 2, (LenY - move * 2) * i / len + move), drawFormat);
            }
        }
        public void qxDrawXLine(Panel pan, float minX, float maxX, int len)
        {
            float move = 20f;
            float LenX = pan.Width;
            float LenY = pan.Height;
            Graphics g = pan.CreateGraphics();
            PointF py1;
            PointF py2;
            string sy;
            Pen pen;
            StringFormat drawFormat = new StringFormat();
            for (int i = 0; i <= len - 1; i++)
            {
                py1 = new PointF((LenX - move * 2) * i / len + move, move);
                py2 = new PointF((LenX - move * 2) * i / len + move, LenY - move);
                sy = ((maxX - minX) * i / len + minX).ToString();
                pen = new Pen(Color.Black, 1);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
                g.DrawLine(pen, py1, py2);
                drawFormat.Alignment = StringAlignment.Far;
                drawFormat.LineAlignment = StringAlignment.Center;
                g.DrawString(sy, new Font("宋体", 9f), Brushes.Black, new PointF((LenX - move * 2) * i / len + move - 6, LenY - move + 2));
            }
            py1 = new PointF((LenX - move * 2) * len / len + move, move);
            py2 = new PointF((LenX - move * 2) * len / len + move, LenY - move);
            sy = ((maxX - minX) * len / len + minX).ToString();
            pen = new Pen(Color.Black, 1);
            g.DrawLine(pen, py1, py2);
            drawFormat.Alignment = StringAlignment.Far;
            drawFormat.LineAlignment = StringAlignment.Center;
            g.DrawString(sy, new Font("宋体", 9f), Brushes.Black, new PointF((LenX - move * 2) + move - 6, LenY - move + 2));
        }
        #endregion
    }
}
